{% extends "blog/base.html" %}
{% load blog_tags %}

{% block title %}搜索{% endblock %}

{% block content %}
    {% if "query" in request.GET %}
        <h1 class="page-title">Search Results for : {{ cd.query }}
            <span class="badge">{{ total_results }}</span>
        </h1>
{#        <h1 class="page-title">Fount {{ total_results }} result{{ total_results|pluralize }}</h1>#}
        {% if total_results > 0 %}
{#            {% for result in results %}#}
{#                {% with post=result.object %}#}
{#                    <h4><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h4>#}
{#                    {{ post.body|truncatewords:5 }}#}
{#                {% endwith %}#}
{#            {% empty %}#}
{#                <h1 class="page-title">There are no results for your query.</h1>#}
{#            {% endfor %}#}

    <div class="post-list">
    {% for post in posts %}
        {% with post=post.object %}
        <div class="title">
            <a href="{{ post.get_absolute_url }}">
                {{ post.title }}
            </a>
        </div>
{#        {{ post.tags.all|join:"; "}}#}
        <div class="date">
            {{ post.publish }} by {{ post.author }}
            <span class="tags">
{#                Tags:#}
                {% for tag in post.tags.all %}
                    <a href="{% url "blog:post_list_by_tag" tag.slug %}">
                        <span class="label label-info">
                            {{ tag.name }}
                        </span>
                    </a>
                    {% if not forloop.last %} &nbsp {% endif %}
                {% endfor %}
            </span>
        </div>

        <div class="post-body">
            {{ post.body|markdown|truncatewords_html:30 }}
            <div class="btn">
            <a class="btn btn-default btn-sm" href="{{ post.get_absolute_url }}">
                Continue reading >>
            </a>
            </div>
        </div>
        {% endwith %}

    {% empty %}
        <h1 class="page-title">There are no results for your query.</h1>
    {% endfor %}
{#    {% include "pagination_search.html" with page=posts %}#}
    <div class="pagination">
    <nav>
        <ul class="pager">
            {% if posts.has_previous %}
                <li class="previous">
                    <a href="?page={{ posts.previous_page_number }}&query={{ cd.query }}"><span aria-hidden="true">&larr;</span> 上一页</a>
                </li>
            {% endif %}
            {% if posts.has_next %}
                <li class="next">
                    <a href="?page={{ posts.next_page_number }}&query={{ cd.query }}">下一页 <span aria-hidden="true">&rarr;</span></a>
                </li>
            {% endif %}
        </ul>
    </nav>
    </div>
{#    <p>{{ posts }}</p>#}

    </div>
{#        <p><a href="{% url "blog:post_search" %}">Search again</a></p>#}
        {% else %}
            <h1 class="page-title">There are no results for your query.</h1>

        <div class="post-list">
        <form action="/blog/search/" class="form-horizontal" method="get" role="search" >
            <div class="form-group col-group-sm">
                <div class="col-lg-12">
                <input type="text" class="form-control" placeholder="Search" name="query" required/>
                    </div>
            </div>
            <button type="submit" class="btn btn-default">
                <span class="col-lg-1">
                    <span class="glyphicon glyphicon-search	"></span>
                </span>
            </button>

        </form>
        </div>

        {% endif %}
            <h1 class="copyright">Welcome to visit my website © Pyx</h1>

    {% else %}
        <h1 class="page-title">Search Results for : {{ cd.query }}
            <span class="badge">0</span>
        </h1>        <!--
        <form action="." method="get">
{#            {{ form.as_p }}#}
            <input type="submit" value="Search">
        </form>-->

        <div class="post-list">
        <form action="/blog/search/" class="form-horizontal" method="get" role="search" >
            <div class="form-group col-group-sm">
                <div class="col-lg-12">
                <input type="text" class="form-control" placeholder="Search" name="query" required/>
                    </div>
            </div>
            <button type="submit" class="btn btn-default">
                <span class="col-lg-1">
                    <span class="glyphicon glyphicon-search	"></span>
                </span>
            </button>

        </form>
        </div>
        <h1 class="copyright">Welcome to visit my website © Pyx</h1>
    {% endif %}
{% endblock %}